#!/bin/bash
### BEGIN INIT INFO
# Provides:          lcd
# Required-Start:    $fpga.sh
# Required-Stop:
# Default-Start:     S
# Default-Stop:
### END INIT INFO
#Define all nodes:

major=225
nodes=("Ctl" "Data" "CtlStr" "DataStr")

lcd_load() {
    #载入Lcd驱动模块
    rmmod lcd.ko
    insmod /home/root/lcd.ko major="${major}"

    i=0
    for node in ${nodes[@]}; do
    nodePath=/dev/lcd$node
    #删除已存在的节点
    rm -f $nodePath
    #创建节点
    mknod $nodePath c $major $i
    echo mknod $nodePath c $major $i
    chmod 666 $nodePath
    ((i+=1))
    done
}

init() {
    echo -n "0x38" > /dev/lcdCtlStr
    echo -n "0x0C" > /dev/lcdCtlStr
    echo -n "0x06" > /dev/lcdCtlStr
}

clear() {
    echo -n "0x01" > /dev/lcdCtlStr
}

print1() {
    echo -n "0x80" > /dev/lcdCtlStr
    echo -n "                                        " > /dev/lcdData
    echo -n "0x80" > /dev/lcdCtlStr
    echo -n $1 > /dev/lcdData
}

print2() {
    echo -n "0xc0" > /dev/lcdCtlStr
    echo -n "                                        " > /dev/lcdData
    echo -n "0xc0" > /dev/lcdCtlStr
    echo -n $1 > /dev/lcdData
}

append() {
    echo -n $1 > /dev/lcdData
}

case "$1" in
  start)
    lcd_load
    init
    clear
    print1 "LCD Boot..."
    print2 "Success"
    ;;
  init)
    init
    clear
    ;;
  clear)
    clear
    ;;
  print1)
    print1 $2
    ;;
  print2)
    print2 $2
    ;;
  append)
    append $2
    ;;
  *)
    echo "Usage: lcd {start|init|clear|print1 'some text to line 1'|print2 'some text to line 2'|append 'some text to current cursor position'}"
    exit 1
esac

exit 0
